<template>
  <div class="app-container">

    <!-- 查询条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item label="仓库" prop="wmsDictWarehouse">
          <el-select v-model="queryParams.wmsDictWarehouse" style="width: 250px" placeholder="请选择仓库" clearable size="small">
            <el-option
              v-for="dict in dict.type.wms_dict_warehouse"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="归还日期">
          <el-date-picker
            v-model="daterangeReturnBackDt"
            size="small"
            style="width: 250px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="单据编号" prop="docNum">
          <el-input
            v-model="queryParams.docNum"
            placeholder="请输入单据编号模糊查询"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            @blur="trimOnBlur" style="width: 250px"
          />
        </el-form-item>

        <el-form-item label="用途" prop="wmsDictUseType">
          <el-select v-model="queryParams.wmsDictUseType" style="width: 250px" placeholder="请选择用途" clearable size="small">
            <el-option
              v-for="dict in dict.type.wms_dict_use_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="queryParams.sysNormalDisable" style="width: 250px" placeholder="请选择状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="单据时间">
          <el-date-picker
            v-model="daterangeDocDt"
            size="small"
            style="width: 250px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="审核时间">
          <el-date-picker
            v-model="daterangeConfirmDt"
            size="small"
            style="width: 250px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="归还计划">
          <el-date-picker
            v-model="daterangeReturnBackSchedule"
            size="small"
            style="width: 250px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="审核结果" prop="oaDictConfirmStatus">
          <el-select v-model="queryParams.oaDictConfirmStatus" style="width: 250px" placeholder="请选择审核结果" clearable size="small">
            <el-option
              v-for="dict in dict.type.oa_dict_confirm_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 批量操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:borrow:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:borrow:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:borrow:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:borrow:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" stripe :data="borrowList" :highlight-current-row="true" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" fixed="left"/>
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="备注">
              <span>{{ scope.row.docNote }}</span>
            </el-form-item>
            <el-form-item label="审核人">
              <span>{{ scope.row.confirmUserNickName }}</span>
            </el-form-item>
            <el-form-item label="审核状态">
              <dict-tag :options="dict.type.oa_dict_confirm_status" :value="scope.row.oaDictConfirmStatus"/>
            </el-form-item>
            <el-form-item label="审核时间">
              <span>{{ scope.row.confirmDt }}</span>
            </el-form-item>
            <el-form-item label="审核意见">
              <span>{{ scope.row.confirmNote }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="仓库" align="center" prop="wmsDictWarehouse" v-if="columns[0].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_dict_warehouse" :value="scope.row.wmsDictWarehouse"/>
        </template>
      </el-table-column>
      <el-table-column label="用途" align="center" prop="wmsDictUseType" v-if="columns[1].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_dict_use_type" :value="scope.row.wmsDictUseType"/>
        </template>
      </el-table-column>
      <el-table-column label="归还计划(日期)" align="center" prop="returnBackSchedule" width="180" v-if="columns[2].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnBackSchedule, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归还日期" align="center" prop="returnBackDt" width="180" v-if="columns[3].visible">
        <template slot-scope="scope">
          <span v-if="scope.row.returnBackDt">{{ parseTime(scope.row.returnBackDt, '{y}-{m}-{d}') }}</span>
          <el-tooltip class="item" effect="dark" content="点击按钮进行物料返还" placement="top-start">
            <el-button
              type="danger"
              plain
              size="mini"
              v-show="!scope.row.returnBackDt"
              @click="showDialog_return(scope.row)">未返还
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="sysNormalDisable" v-if="columns[4].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.sysNormalDisable"/>
        </template>
      </el-table-column>
      <el-table-column label="单据编号" align="center" prop="docNum" width="150" v-if="columns[5].visible"/>
      <el-table-column label="单据时间" align="center" prop="docDt" width="180" v-if="columns[6].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.docDt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="attentionUserNickName" v-if="columns[7].visible"/>
      <el-table-column label="审核状态" align="center" prop="oaDictConfirmStatus" v-if="columns[8].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.oa_dict_confirm_status" :value="scope.row.oaDictConfirmStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:borrow:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-claim"
            @click="showDialog_return(scope.row)"
            v-hasPermi="['wms:borrow:edit']"
          >返还
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:crmOrders:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="showDialog_confirm(scope.row)"
            v-hasPermi="['mes:process:remove']"
          > 审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框：物料借用 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="1050px" append-to-body :close-on-click-modal=false>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-alert v-show="form.oaDictConfirmStatus != 'editing'"
                  style="margin-bottom: 15px;"
                  :title="form.latestTaskInfo"
                  type="info"
                  :closable="false"
                  effect="dark"
        >
        </el-alert>
        <!--基本信息 -->
        <el-card class="box-card" shadow="hover" style="margin-bottom: 20px;">
          <div slot="header" class="clearfix">
            <span><b>基本信息</b></span>
            <el-tooltip content="折叠 / 展开" placement="top" style="float: right; padding: 3px 0">
              <el-switch
                v-model="fold_form_borrow"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="false"
                inactive-value="true">
              </el-switch>
            </el-tooltip>
          </div>
          <div v-show="fold_form_borrow== 'false'">

            <el-col :span="8">
              <el-form-item label="仓库" prop="wmsDictWarehouse">
                <el-select v-model="form.wmsDictWarehouse" placeholder="请选择仓库" :style="{width: '90%'}"
                           :disabled="form.oaDictConfirmStatus != 'editing'">
                  <el-option
                    v-for="dict in dict.type.wms_dict_warehouse"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="docDt">
                <span slot="label">
                  借用时间
                  <el-tooltip content="保留到分钟" placement="top">
                    <i class="el-icon-question"></i>
                  </el-tooltip>
                </span>
                <el-date-picker clearable size="small"
                                v-model="form.docDt"
                                type="datetime"
                                placeholder="设置借用时间" :style="{width: '90%'}"
                                :disabled="form.oaDictConfirmStatus != 'editing'">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="状态">
                <el-radio-group v-model="form.sysNormalDisable">
                  <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                    :disabled="form.oaDictConfirmStatus != 'editing'"
                  >{{dict.label}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>

            </el-col>

            <el-col :span="8">


              <el-form-item label="归还计划" prop="returnBackSchedule">
                <el-date-picker clearable size="small" :style="{width: '90%'}"
                                v-model="form.returnBackSchedule"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择归还计划（日期）"
                                :disabled="form.oaDictConfirmStatus != 'editing'">
                </el-date-picker>
              </el-form-item>

              <el-form-item prop="fkAttentionUserId" :disabled="form.oaDictConfirmStatus != 'editing'">
                <span slot="label">
                  经办人
                  <el-tooltip content="格式：姓名（用户ID）" placement="top">
                    <i class="el-icon-question"></i>
                  </el-tooltip>
                </span>
                <el-input v-model="form.fkAttentionUserId" v-show="false"/>
                <el-input placeholder="请选择经办人" readOnly="true"
                          v-model="form.attentionUser"
                          class="input-with-select" :style="{width: '90%'}"
                          :disabled="form.oaDictConfirmStatus != 'editing'">
                  <el-button @click="showDialog_selectUser" slot="append" icon="el-icon-search"
                             :disabled="form.oaDictConfirmStatus != 'editing'"></el-button>
                </el-input>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="单据编号" prop="docNum">
                <el-input v-model="form.docNum" placeholder="请输入单据编号"
                          :style="{width: '100%'}" :disabled="form.oaDictConfirmStatus != 'editing'"
                          @blur="trimOnBlur"/>
              </el-form-item>

              <el-form-item label="用途" prop="wmsDictUseType">
                <el-select v-model="form.wmsDictUseType" placeholder="请选择用途" :style="{width: '90%'}"
                           :disabled="form.oaDictConfirmStatus != 'editing'">
                  <el-option
                    v-for="dict in dict.type.wms_dict_use_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="备注" prop="docNote" :disabled="form.oaDictConfirmStatus != 'editing'">
                <el-input type="textarea" autosize v-model="form.docNote" placeholder="请输入备注信息" @blur="trimOnBlur"
                          :maxlength="1000" show-word-limit clearable
                          prefix-icon='el-icon-edit' :style="{width: '100%'}"
                          :disabled="form.oaDictConfirmStatus != 'editing'"></el-input>
              </el-form-item>
            </el-col> <!-- 备注 -->

          </div>
        </el-card>

        <!--借用明细 -->
        <el-card class="box-card" shadow="hover" style="margin-bottom: 20px;">
          <div slot="header" class="clearfix">
            <span><b>借用明细</b></span>
          </div>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWmsBorrowDetails"
                         :disabled="form.oaDictConfirmStatus != 'editing'">
                添加
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWmsBorrowDetails"
                         :disabled="form.oaDictConfirmStatus != 'editing'">
                删除
              </el-button>
            </el-col>
          </el-row>

          <el-table :data="wmsBorrowDetailsList" :row-class-name="rowWmsBorrowDetailsIndex"
                    @selection-change="handleWmsBorrowDetailsSelectionChange" ref="wmsBorrowDetails">
            <el-table-column type="selection" width="50" align="center"
                             :disabled="form.oaDictConfirmStatus != 'editing'"/>
            <el-table-column label="排序序号" prop="orderNum">
              <template slot-scope="scope">
                <el-input v-model="scope.row.orderNum"
                          placeholder="请输入排序序号"
                          :disabled="form.oaDictConfirmStatus != 'editing'"
                          @blur="trimOnBlur"/>
              </template>
            </el-table-column>

            <!-- 物料 -->
            <el-table-column label="物料名称" prop="itemName" width="300px" align="center"
                             :disabled="form.oaDictConfirmStatus != 'editing'">
              <template slot-scope="scope">
                <el-input v-model="scope.row.fkItemUuid" v-show="false"/>
                <el-input placeholder="请选择物料" readOnly="true" v-model="scope.row.selectItemName "
                          class="input-with-select" :disabled="form.oaDictConfirmStatus != 'editing'">
                  <el-button @click="dialog_show_selectItem(scope)" slot="append" icon="el-icon-search"
                             :disabled="form.oaDictConfirmStatus != 'editing'"></el-button>
                </el-input>
              </template>
            </el-table-column>

            <el-table-column label="借用数量" prop="borrowQty">
              <template slot-scope="scope">
                <el-input v-model="scope.row.borrowQty"
                          placeholder="请输入借用数量"
                          :disabled="form.oaDictConfirmStatus != 'editing'"
                          @blur="trimOnBlur"/>
              </template>
            </el-table-column>


            <el-table-column label="计量单位" prop="wmsDictMeasuringUnit" width="200px" align="center">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.wmsDictMeasuringUnit"
                  placeholder="请选择"
                  style="width: 150px"
                  clearable
                  filterable
                  :disabled="form.oaDictConfirmStatus != 'editing'"
                >
                  <el-option
                    v-for="dict in measuringUnitOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </template>
            </el-table-column>

          </el-table>

        </el-card>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="form.oaDictConfirmStatus != 'editing'">确 定</el-button>
        <el-button type="warning" @click="submit_confirm" :disabled="form.oaDictConfirmStatus != 'editing'" v-show="false">提交待审
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 对话框：返还 return -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="dialog_title_return" :visible.sync="dialog_visible_return"
               width="800px" append-to-body :close-on-click-modal=false>
      <el-form ref="form_return" :model="form_return" :rules="rules_return" label-width="100px">

        <!--基本信息 -->
        <el-card class="box-card" shadow="hover" style="margin-bottom: 20px;">
          <div slot="header" class="clearfix">
            <b>基本信息</b>
            <el-tooltip content="折叠 / 展开" placement="top" style="float: right; padding: 3px 0">
              <el-switch
                v-model="fold_form_borrow"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="false"
                inactive-value="true">
              </el-switch>
            </el-tooltip>
          </div>
          <div v-show="fold_form_borrow== 'false'">
            <el-col :span="8">
              <el-form-item label="仓库">{{ formatDict_wms_dict_warehouse(form_return) }}</el-form-item>
              <el-form-item label="借用时间"> {{ form_return.docDt }}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="用途">{{ formatDict_wms_dict_use_type(form_return) }}</el-form-item>
              <el-form-item label="归还计划"> {{ form_return.returnBackSchedule }}</el-form-item>

            </el-col>
            <el-col :span="8">
              <el-form-item label="单据编号"> {{ form_return.docNum }}</el-form-item>
<!--              <el-form-item label="状态">{{ formatDict_sys_normal_disable(form_return) }}</el-form-item>-->
              <el-form-item label="经办人">
                {{ form_return.attentionUserNickName + '（' + form_return.fkAttentionUserId + '）' }}
              </el-form-item>
            </el-col>
<!--            <el-col :span="8">-->
<!--              <el-form-item label="备注">{{ form_return.docNote }}</el-form-item>-->
<!--            </el-col> &lt;!&ndash; 备注 &ndash;&gt;-->
          </div>
        </el-card>

        <!--借用明细 -->
        <el-card class="box-card" shadow="hover" style="margin-bottom: 20px;">
          <div slot="header" class="clearfix">
            <b>借用明细</b>
          </div>

          <el-table :data="detailsList_return" :row-class-name="rowWmsBorrowDetailsIndex"
                    @selection-change="handleWmsBorrowDetailsSelectionChange" ref="wmsBorrowDetails">

            <el-table-column label="序号" align="center" prop="orderNum"/>

            <!-- 物料 -->
            <el-table-column label="物料名称" prop="itemName" width="120px" align="center">
              <template slot-scope="scope">
                {{ scope.row.itemName }}
              </template>
            </el-table-column>

            <el-table-column label="物料编码" prop="itemNum" width="160px" align="center">
              <template slot-scope="scope">
                {{ scope.row.itemNum }}
              </template>
            </el-table-column>

            <el-table-column label="借用数量" align="center" prop="borrowQty"/>


            <el-table-column label="计量单位" align="center" prop="wmsDictMeasuringUnit">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.wms_dict_measuring_unit" :value="scope.row.wmsDictMeasuringUnit"/>
              </template>
            </el-table-column>

          </el-table>

        </el-card>

        <!-- 返还日期 -->
        <el-card class="box-card" shadow="hover" style="margin-bottom: 20px;">
          <div slot="header" class="clearfix">
            <span><b>返还日期</b></span>
          </div>

          <el-col :span="8">
            <el-form-item prop="returnBackDt">
                <span slot="label">
                  返还日期
                  <el-tooltip content="保留到分钟" placement="top">
                    <i class="el-icon-question"></i>
                  </el-tooltip>
                </span>
              <el-date-picker clearable size="small"
                              v-model="form_return.returnBackDt"
                              type="datetime"
                              placeholder="设置返还时间" :style="{width: '90%'}">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click="submitForm_return">提交</el-button>
            </el-form-item>
          </el-col>

        </el-card>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideDialog_return">取 消</el-button>
      </div>
    </el-dialog>

    <select-user ref="component_selectUser" :roleId="queryParams.roleId" @ok="selectUser_done"/>
    <select-item ref="component_selectItem" @ok="selectItem_done"/>
    <confirm ref="component_confirm" @ok="confirm_done"/>

  </div>
</template>

<script>
  import {listBorrow, getBorrow, delBorrow, addBorrow, updateBorrow} from "@/api/wms/borrow";
  import selectItem from "../../wms/wmsItems/selectItem";
  import selectUser from "../../comn/userGroup/selectUser";
  import confirm from "../../oa/confirm";
  import {newDocNum} from "@/api/oa/docNum";
  import {trimOnBlur} from '@/utils/form'

  export default {
    name: "Borrow",
    components: {selectItem, selectUser, confirm},
    dicts: ['wms_dict_warehouse', 'wms_dict_use_type', 'sys_normal_disable', 'oa_dict_confirm_status'
      , 'wms_dict_measuring_unit',],
    data() {
      return {

        fold_form_borrow: 'false',
        measuringUnitOptions: [],
        dialog_visible_return: false,
        dialog_title_return: '',

        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedWmsBorrowDetails: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 物料借用表格数据
        borrowList: [],
        // 物料借用单明细表格数据
        wmsBorrowDetailsList: [],
        detailsList_return: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 计量单位时间范围
        daterangeReturnBackSchedule: [],
        // 计量单位时间范围
        daterangeReturnBackDt: [],
        // 计量单位时间范围
        daterangeDocDt: [],
        // 计量单位时间范围
        daterangeConfirmDt: [],
        // 计量单位时间范围
        daterangeCreateTime: [],
        // 计量单位时间范围
        daterangeUpdateTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          wmsDictWarehouse: null,
          wmsDictUseType: null,
          returnBackSchedule: null,
          returnBackDt: null,
          sysNormalDisable: null,
          docTitle: null,
          docNum: null,
          docDt: null,
          docNote: null,
          fkAttentionUserId: null,
          fkConfirmUserId: null,
          confirmDt: null,
          oaDictConfirmStatus: null,
          confirmNote: null,
        },

        // 表单参数
        form: {},
        form_return: {},

        // 表单校验
        rules_return: {
          returnBackDt: [
            {required: true, message: "返还日期不能为空", trigger: "change"}
          ],
        },
        rules: {
          wmsDictWarehouse: [
            {required: true, message: "请选择仓库", trigger: "change"}
          ],
          wmsDictUseType: [
            {required: true, message: "请选择用途", trigger: "change"}
          ],
          docNum: [
            {required: true, message: "请输入单据编号", trigger: "change"}
          ],
          docDt: [
            {required: true, message: "请输入借用时间", trigger: "change"}
          ],
          fkAttentionUserId: [
            {required: true, message: "请选择经办人", trigger: "change"}
          ],
          returnBackSchedule: [
            {required: true, message: "请选择归还计划", trigger: "change"}
          ],
        },
        columns: [
          {key: 0, label: `仓库`, visible: true},
          {key: 1, label: `用途`, visible: true},
          {key: 2, label: `归还计划(日期)`, visible: true},
          {key: 3, label: `归还日期`, visible: true},
          {key: 4, label: `状态`, visible: true},
          {key: 5, label: `单据编号`, visible: true},
          {key: 6, label: `单据时间`, visible: true},
          {key: 7, label: `经办人`, visible: true},
          {key: 8, label: `审核状态`, visible: true},
        ],

      };
    },
    created() {
      this.getList();

      /**获取计量单位字典数据*/
      this.getDicts("wms_dict_measuring_unit").then(response => {
        this.measuringUnitOptions = response.data;
      });


    },
    methods: {
      trimOnBlur,
      /** 查询物料借用列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeReturnBackSchedule && '' != this.daterangeReturnBackSchedule) {
          this.queryParams.params["beginReturnBackSchedule"] = this.daterangeReturnBackSchedule[0];
          this.queryParams.params["endReturnBackSchedule"] = this.daterangeReturnBackSchedule[1];
        }
        if (null != this.daterangeReturnBackDt && '' != this.daterangeReturnBackDt) {
          this.queryParams.params["beginReturnBackDt"] = this.daterangeReturnBackDt[0];
          this.queryParams.params["endReturnBackDt"] = this.daterangeReturnBackDt[1];
        }
        if (null != this.daterangeDocDt && '' != this.daterangeDocDt) {
          this.queryParams.params["beginDocDt"] = this.daterangeDocDt[0];
          this.queryParams.params["endDocDt"] = this.daterangeDocDt[1];
        }
        if (null != this.daterangeConfirmDt && '' != this.daterangeConfirmDt) {
          this.queryParams.params["beginConfirmDt"] = this.daterangeConfirmDt[0];
          this.queryParams.params["endConfirmDt"] = this.daterangeConfirmDt[1];
        }
        if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
          this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
          this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
        }
        if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
          this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
          this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
        }
        listBorrow(this.queryParams).then(response => {
          this.borrowList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          borrowUuid: null,
          wmsDictWarehouse: null,
          wmsDictUseType: null,
          returnBackSchedule: null,
          returnBackDt: null,
          sysNormalDisable: '0',
          docTitle: null,
          docNum: null,
          docDt: null,
          docNote: null,
          fkAttentionUserId: null,
          attentionUserNickName: '',
          fkConfirmUserId: null,
          confirmDt: null,
          oaDictConfirmStatus: 'editing',
          confirmNote: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.wmsBorrowDetailsList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeReturnBackSchedule = [];
        this.daterangeReturnBackDt = [];
        this.daterangeDocDt = [];
        this.daterangeConfirmDt = [];
        this.daterangeCreateTime = [];
        this.daterangeUpdateTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.borrowUuid)
        this.docNums = selection.map(item => item.docNum)
        this.single = selection.length !== 1
        this.oaDictConfirmStatus = selection.map(i => i.oaDictConfirmStatus)
        let editing = 'editing';
        this.multiple =
          (this.oaDictConfirmStatus.indexOf(editing) > -1 && [...new Set(this.oaDictConfirmStatus)] == editing) ? false : true
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();

        newDocNum({preCode:'BOR-', len: 12}).then(response => {
          this.form.docNum = response.msg
          console.log(this.form.docNum)
        })

        this.form.oaDictConfirmStatus = 'editing'
        this.open = true;
        this.title = "添加物料借用";
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const borrowUuid = row.borrowUuid || this.ids
        getBorrow(borrowUuid).then(response => {
          this.form = response.data
          // 审核
          this.form.oaDictConfirmStatus =
            ('editing' == this.form.oaDictConfirmStatus || 'reject' == this.form.oaDictConfirmStatus)
              ? 'editing' : this.form.oaDictConfirmStatus

          if (row.oaDictConfirmStatus != 'editing' || row.oaDictConfirmStatus != 'reject') {
            this.form.latestTaskInfo = '💡 该条记录审核状态为' +
              this.selectDictLabel(this.dict.type.oa_dict_confirm_status, row.oaDictConfirmStatus) + '状态' + ',' + '不能修改'
          }

          this.wmsBorrowDetailsList = this.form.wmsBorrowDetailsList;
          this.open = true;
          this.title = "修改物料借用";

        });
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.wmsBorrowDetailsList = this.wmsBorrowDetailsList;
            if (this.form.borrowUuid != null) {
              updateBorrow(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBorrow(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      submit_confirm() {
        this.form.oaDictConfirmStatus = 'waiting'
        updateBorrow(this.form).then(response => {
          this.$modal.msgSuccess("提交待审成功");
          this.open = false;
          this.getList();
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const borrowUuids = row.borrowUuid || this.ids;
        const docNums = row.docNum || this.docNums;
        this.$modal.confirm('是否确认删除单据编号为"'+docNums+'"的借用单？', '删除确认').then(function () {
          return delBorrow(borrowUuids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 物料借用单明细序号 */
      rowWmsBorrowDetailsIndex({row, rowIndex}) {
        row.index = rowIndex + 1;
      },
      /** 物料借用单明细添加按钮操作 */
      handleAddWmsBorrowDetails() {
        let obj = {};
        obj.orderNum = "1";
        obj.itemNum = "";
        obj.selectItemName = "";
        obj.itemName = "";
        obj.fkItemUuid = "";
        obj.borrowQty = "1";
        obj.wmsDictMeasuringUnit = "";
        this.wmsBorrowDetailsList.push(obj);
      },
      /** 物料借用单明细删除按钮操作 */
      handleDeleteWmsBorrowDetails() {
        if (this.checkedWmsBorrowDetails.length == 0) {
          this.$modal.msgError("请先选择要删除的物料借用单明细数据");
        } else {
          const wmsBorrowDetailsList = this.wmsBorrowDetailsList;
          const checkedWmsBorrowDetails = this.checkedWmsBorrowDetails;
          this.wmsBorrowDetailsList = wmsBorrowDetailsList.filter(function (item) {
            return checkedWmsBorrowDetails.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleWmsBorrowDetailsSelectionChange(selection) {
        this.checkedWmsBorrowDetails = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wms/borrow/export', {
          ...this.queryParams
        }, `wms_borrow.xlsx`)
      },

      // - - - - - - - - - - - - - - - - - - - - - - - -
      // 组件：选择用户
      // - - - - - - - - - - - - - - - - - - - - - - - -

      /** 对话框：选择用户 */
      showDialog_selectUser() {
        this.$refs.component_selectUser.show();
      },

      /** 选择用户完成 */
      selectUser_done(user) {
        console.log(user)
        this.form.fkAttentionUserId = user.userId
        this.form.attentionUserNickName = user.nickName
        this.form.attentionUser = `${user.nickName}（${user.userId}）`
      },

      // - - - - - - - - - - - - - - - - - - - - - - - -
      // 组件：选择物料
      // - - - - - - - - - - - - - - - - - - - - - - - -

      /** 显示对话框：选择物料 */
      dialog_show_selectItem(scope) {
        console.log(scope)
        this.currRowScope = scope
        this.$refs.component_selectItem.dialog_show_selectItem();
      },

      /** 完成：选择物料 */
      selectItem_done(item) {
        let row = this.currRowScope.row
        row.selectItemName = `${item.itemName}(物料编号:${item.itemNum})`
        row.fkItemUuid = item.itemUuid
      },

      // - - - - - - - - - - - - - - - - - - - - - - - -
      // 审核 confirm
      // - - - - - - - - - - - - - - - - - - - - - - - -

      /** 打开对话框：审核 */
      showDialog_confirm(row) {
        let obj = {
          tableName: 'wms_borrow',
          uuidColumn: 'borrow_uuid',
          fkUuid: row.borrowUuid,
          attentionUserNickName: row.attentionUserNickName,
          msg: `请审核物料借用申请, 单据编号：${row.docNum}`
        }
        this.$refs.component_confirm.showDialog_confirm(obj);
      },

      confirm_done() {
        this.getList()
      },

      // - - - - - - - - - - - - - - - - - - - - - - - -
      // 返还 return
      // - - - - - - - - - - - - - - - - - - - - - - - -

      /** 表单重置：返还 */
      resetForm_return() {
        this.form_return = {
          returnBackDt: null,
        };
        this.detailsList_return = [];
        this.resetForm("form_return");
      },

      /** 显示对话框：返还 */
      showDialog_return(row) {
        this.resetForm_return();
        const borrowUuid = row.borrowUuid || this.ids
        getBorrow(borrowUuid).then(response => {
          this.form_return = response.data
          this.detailsList_return = this.form_return.wmsBorrowDetailsList;
          this.dialog_visible_return = true;
          this.dialog_title_return = "物料返还";

        });
      },

      /** 隐藏对话框：返还 */
      hideDialog_return() {
        this.dialog_visible_return = false
      },

      /** 提交表单：返还 */
      submitForm_return() {
        this.$refs["form_return"].validate(valid => {
          if (valid) {
            this.form_return.wmsBorrowDetailsList = this.detailsList_return;
            updateBorrow(this.form_return).then(response => {
              this.$modal.msgSuccess("返还成功");
              this.dialog_visible_return = false;
              this.getList();
            });
          }
        });
      },

      // - - - - - - - - - - - - - - - - - - - - - - - -
      // 字典转译 dict
      // - - - - - - - - - - - - - - - - - - - - - - - -

      formatDict_wms_dict_use_type(row, column) {
        return this.selectDictLabel(this.dict.type.wms_dict_use_type, row.wmsDictUseType);
      },

      formatDict_wms_dict_warehouse(row, column) {
        return this.selectDictLabel(this.dict.type.wms_dict_warehouse, row.wmsDictWarehouse);
      },

      formatDict_sys_normal_disable(row, column) {
        return this.selectDictLabel(this.dict.type.sys_normal_disable, row.sysNormalDisable);
      },


    }
  };
</script>
